package com.hqj.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * spring属性初始化练习02
 * @author HQJ
 * 1、有内存条接口Memory,获取大小的方法getSize()
 *    有内存实现类Kingston,重写方法并返回内存大小及名称 如“Kingston DDR3 8G”
 *
 * 2、有处理器接口CUP,获取配置的方法getCores()
 *    有处理器实现类Inter,重写方法并返回配置信息，如“Inter I7 5代 8核”
 *
 * 3、有类Computer，有属性Memory,CUP,String brand（品牌）
 *    get/set  构造方法
 *
 * 4、有商铺类shop,属性List<Computer> list 保存几台电脑
 *    get/set方法
 *    方法showComputer();输出电脑的配置
 *    如:品牌:
 *       cup:
 *       内存:
 *
 * 5、测试类，输出商铺的存储电脑
 * 新建spring02.xml配置文件，把上述内容，通过配置bean的方式进行注入，不能使用new
 *
 *
 */
public class Exercise02 {
    public static void main(String[] args) {
        //读取配置文件
        ApplicationContext ac =
                new ClassPathXmlApplicationContext("com/hqj/demo/spring02.xml");
        Shop shop = (Shop)ac.getBean("shop");
        shop.showComputer();
    }
}
interface Memory{
    public String getSize();
}
class Kingston implements Memory{
    @Override
    public String getSize() {
        return "Kingston DDR3 8G";
    }
}
interface CPU{
    public String getCores();
}
class Inter implements CPU{
    @Override
    public String getCores() {
        return "Inter I7 5代 8核";
    }
}
class Computer{
    private CPU cpu;
    private Memory memory;
    private String brand;

    public Computer() {}

    public Computer(CPU cpu, Memory memory, String brand) {
        this.cpu = cpu;
        this.memory = memory;
        this.brand = brand;
    }

    public CPU getCpu() {
        return cpu;
    }

    public void setCpu(CPU cpu) {
        this.cpu = cpu;
    }

    public Memory getMemory() {
        return memory;
    }

    public void setMemory(Memory memory) {
        this.memory = memory;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    @Override
    public String toString() {
        return "Computer{" +
                "cpu=" + cpu +
                ", memory=" + memory +
                ", brand='" + brand + '\'' +
                '}';
    }
}
class Shop{
    private List<Computer> list;

    public List<Computer> getList() {
        return list;
    }

    public void setList(List<Computer> list) {
        this.list = list;
    }

    public void showComputer(){
        System.out.println("电脑有:");
        for (Computer c:list){
            System.out.println("品牌:"+c.getBrand()+"\ncpu:"+c.getCpu().getCores()
                    +"\n内存条:"+c.getMemory().getSize());
            System.out.println("---------------------------");
        }
    }
}